from pyspark import SparkContext,SparkConf

# 创建spark配置对象
conf=SparkConf().setAppName('MyApp').setMaster('local')

# 创建sparkContext对象
sc=SparkContext(conf=conf)

# 加载数据
lines=sc.textFile('D:\products\python\others\data.txt')

# 将每一行分割成单词
words=lines.flatMap(lambda line: line.split(' '))

# 将每个单词映射成键值对
words_count=words.map(lambda word: (word,1))

# 统计每个单词出现的次数
counts=words_count.reduceByKey(lambda a,b:a+b)

# 收集结果到本地
output=counts.collect()

# 打印输出
for(word,count) in output:
    print(f"{word}: {count}")

# 释放sparkContext
sc.stop()

